package 动态规划.另类的;

import java.util.Arrays;

/**
 * @author aodre , QQ : 480029069
 * @date 2023/3/22 22:06
 */
public class leetcode128最长的连续序列 {


    /*
      这个题, 有点 考察思维!
      而且解法很多! (大厂经典 手撕面试题)
      https://leetcode.cn/problems/longest-consecutive-sequence/solutions/1176496/xiao-bai-lang-ha-xi-ji-he-ha-xi-biao-don-j5a2/
     */



    /*
 时间复杂度 O(NlogN) 排序
 */
    public int longestConsecutive(int[] nums) {
        if(nums.length == 0){
            return 0;
        }
        Arrays.sort(nums);
        int ans = 1, count = 1;
        for(int i = 1;i < nums.length;i++){
            if(nums[i] - nums[i - 1] == 1){
                count++;
                ans = Math.max(ans,count);
            }else if(nums[i] == nums[i - 1]){
                continue;
            }else{
                count = 1;
            }
        }
        return ans;
    }

}
